package com.cloudkinto.base.error;

import com.cloudkinto.common.error.ErrorEnum;
import com.cloudkinto.common.exception.BizException;
import com.cloudkinto.common.i18n.ChineseMessageFactory;
import com.cloudkinto.common.i18n.EnglishMessageFactory;
import com.cloudkinto.common.i18n.JapaneseMessageFactory;
import com.cloudkinto.common.utils.StringsUtil;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.session.UserSessionException;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Calendar;

/**
 * @author zhangyy
 * @date 2020/4/19 15:39
 * @description:全局异常处理
 */
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);


    /**
     * 校验异常处理
     *
     * @param response
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public SingleResult validException(HttpServletResponse response, HttpServletRequest request, MethodArgumentNotValidException e) {
        logger.error("校验异常！原因是：{}", e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        return SingleResult.failure(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * validation异常
     * @param response
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(BindException.class)
    public SingleResult bindException(HttpServletResponse response, HttpServletRequest request, BindException e) {
        logger.error("校验异常！原因是：{}", e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
        return SingleResult.failure(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
    }

    /**
     * 处理自定义的业务异常
     */
    @ExceptionHandler(value = BizException.class)
    public SingleResult bizExceptionHandler(BizException e) {
        logger.error("发生业务异常！原因是：" + StringsUtil.createI18Message(e.getErrorMsg(), e.getArgs()));
        return SingleResult.failure(e.getErrorCode(), e.getErrorMsg(), null, e.getArgs());
    }

    /**
     * 用户会话异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = UserSessionException.class)
    public SingleResult userSessionExceptionHandler(HttpServletRequest request, UserSessionException e) {
        logger.error("发生用户会话异常！原因是：{}", e.getErrorMsg());
        return SingleResult.failure(e.getErrorCode(), e.getMessage());
    }


    /**
     * 处理其他异常
     *
     * @param request
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public SingleResult exceptionHandler(HttpServletRequest request, Exception e) {
        logger.error("未知异常！原因是:", e);
        return SingleResult.failure(ErrorEnum.INTERNAL_SERVER_ERROR);
    }
}
